package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.FormasAgregarForm;
import com.mastercard.assets.web.struts.forms.MaterialesAgregarForm;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MaterialesAgregarRegistrar extends Action {
    public ActionForward execute ( 
            ActionMapping mapping ,
            ActionForm form ,
            HttpServletRequest request , 
            HttpServletResponse response )
            throws IOException , ServletException {
        
        HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
        
                
                MaterialesAgregarForm f = ( MaterialesAgregarForm ) form ;
                
                int asset = f.getCveAsset( ) ;
                
                TblAsset assetTbl = MasterCardDAO.existActiveAssetByNameWithoutId( asset ) ;
                MasterCardDAO.closeEntityManager( ) ;
                
                int cveMaterial = 0 ;
                
                try
		{
			//cveMaterial = MasterCardDAO.addNewMaterial( f.getNombre( ) , f.getDescripcion( ) , f.getArchivoForma( ).getFileName( ) , assetTbl ) ;
		}catch( Exception e )
		{
			user.setMessageError( e.getMessage( ) ) ;
			
			e.printStackTrace( ) ;
			
			user.setNextWorkArea( "/WEB-INF/pages/WorkArea/FormasError.jsp" ) ;
			
			return mapping.findForward( "success" ) ;
		}
                try
                {
                    // en caso de que no haya error
                    // guarda el archivo
                    // nombre d ela forma
                    String filePath = MCUtil.extractMaterialFileName( cveMaterial , f.getArchivoForma( ).getFileName( ) ) ;
                                
                    // abre el archivo
                    File materialDir = new File( MCUtil.getMaterialdir( ) ) ;
                    if( ! materialDir.exists( ) )
                                    materialDir.mkdirs( ) ;

                    File materialFile = new File( materialDir , filePath ) ;
                    System.out.println( "Ruta del material: " + materialFile.getAbsolutePath( ) ) ;
                    FileOutputStream out = new FileOutputStream(  materialFile , false ) ;
                    out.write( f.getArchivoForma( ).getFileData( ) ) ;
                    out.close( ) ;

                    System.out.println( "Cambia el nextWorkArea a /Assets.do" ) ;
                    user.setNextWorkArea( "/Assets.do" ) ;

                    return mapping.findForward( "success" ) ;
                }catch( Exception e )
		{
			e.printStackTrace( ) ;
			try
			{
				//MasterCardDAO.removeFileFromMaterial( cveMaterial ) ;
			}catch( Exception e1 )
			{
				
			}
			
			user.setMessageError( e.getMessage( ) ) ;
			
			user.setNextWorkArea( "/WEB-INF/pages/WorkArea/FormasError.jsp" ) ;
			
			return mapping.findForward( "success" ) ;
		}
                
    } 
    
}
